package com.atguigu.edu.publisher.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.edu.publisher.beans.TrafficChViewDate;
import com.atguigu.edu.publisher.beans.TrafficKeyWordDate;
import com.atguigu.edu.publisher.service.TrafficStatsService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/traffic")
public class TrafficStatsController {

    @Autowired
    TrafficStatsService trafficStatsService;

    //获取关键词粒度评分
    @RequestMapping("/keyWord")
    public String getKeyWord(@RequestParam(value = "date" , defaultValue = "1") String date){
        if ("1".equals(date)){
            date = DateFormatUtils.format(new Date(),"yyyyMMdd");
        }
        List<TrafficKeyWordDate> keyWordDate = trafficStatsService.getKeyWordDate(date);

        HashMap<String, Object> keyWord = new HashMap<>();
        keyWord.put("status",0);

        ArrayList<Object> datas = new ArrayList<>();
        for (TrafficKeyWordDate trafficKeyWordDate : keyWordDate) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("name",trafficKeyWordDate.getKeyWord());
            data.put("value",trafficKeyWordDate.getWordSum());
            datas.add(data);
        }
        keyWord.put("data", datas);
        return JSON.toJSONString(keyWord);
    }

    //获取来源粒度独立访客数
    @RequestMapping("/uvCt")
    public String getUvCt(@RequestParam(value = "date" , defaultValue = "1") String date) {

        if ("1".equals(date)){
            date = DateFormatUtils.format(new Date(),"yyyyMMdd");
        }

        List<TrafficChViewDate> chViewDate = trafficStatsService.getChViewDate(date);

        HashMap<String, Object> uvCtMap = new HashMap<>();
        uvCtMap.put("status",0);

        HashMap<String, Object> data = new HashMap<>();
        List<String> categories = new ArrayList<>();
        for (TrafficChViewDate trafficChViewDate : chViewDate) {
            categories.add(trafficChViewDate.getCh());
        }
        data.put("categories", categories);
        ArrayList<Object> series = new ArrayList<>();
        HashMap<String, Object> series1 = new HashMap<>();
        series1.put("name","来源");
        ArrayList<Long> serData = new ArrayList<>();
        for (TrafficChViewDate trafficChViewDate : chViewDate) {
            serData.add(trafficChViewDate.getUvCt());
        }
        series1.put("data", serData);
        series.add(series1);
        data.put("series", series);
        uvCtMap.put("data", data);

        return JSON.toJSONString(uvCtMap);
    }
}






















